package com.ayz.gateway.core.session.processor;

import lombok.Data;

import java.util.Collection;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * @ClassName RpcPostProcessorRegistry
 * @Description TODO: RPC的增强器的注册器
 * @Author 快乐的星球
 * @Date 2023/5/10 21:26
 * @Version 1.0
 **/
@Data
public class RpcPostProcessorRegistry {
    /**
     * 获取PostProcessor容器
     */
    private final TreeSet<RpcPostProcessor> postProcessors=new TreeSet<>(new Comparator<RpcPostProcessor>() {
        @Override
        public int compare(RpcPostProcessor o1, RpcPostProcessor o2) {
            return o1.getOrder()-o2.getOrder();
        }
    });

    public RpcPostProcessorRegistry(Collection<RpcPostProcessor> postProcessors){
        postProcessors.addAll(postProcessors);
    }

    public RpcPostProcessorRegistry() {
    }

    public void registryRpcPostProcess(RpcPostProcessor postProcessor){
        postProcessors.add(postProcessor);
    }

    public RpcPostProcessor[] getOrderedPostProcessors(){
        TreeSet<RpcPostProcessor> postProcessors = this.postProcessors;
        return postProcessors.toArray(new RpcPostProcessor[0]);
    }
}
